package com.lhb.boyixue.base;

import io.jsonwebtoken.security.Keys;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.crypto.SecretKey;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 86131
 * @version 1.0
 * @description 令牌参数
 * @date 2025/3/29 下午11:58
 */
@Data
@Configuration
public class JwtConfig {
    @Value("${jwt.secret}")
    private  String secret;

    @Value("${jwt.expire}")
    private long expire;

    @Value("${jwt.issuer}")
    private String issuer;
    //真正密钥
    private SecretKey secretKey;

    @PostConstruct
    public void init() {
        // 使用 SHA-256 哈希生成 32 字节密钥
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] hash = digest.digest(secret.getBytes());
        this.secretKey = Keys.hmacShaKeyFor(hash);
    }

}
